import requests
import subprocess
from functools import partial
import os
import json


def get_data():
    url = 'https://www.endata.com.cn/API/GetData.ashx'
    data = {
        'year': '2024',
        'MethodName': 'BoxOffice_GetYearInfoData'
    }
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0'
    }
    response = requests.post(url, data=data, headers=headers)
    return response


if __name__ == '__main__':
    subprocess.Popen = partial(subprocess.Popen, encoding='utf-8')
    import execjs
    response = get_data()
    res = response.text
    os.environ['EXECJS_RUNTIME'] = 'PhantomJS'
    result = execjs.compile(open('main.js', encoding='utf-8').read()).call('main', res)
    data = json.loads(result)['Data']['Table']
    for row in data:
        print(row)
